import { WeaForm, WeaTableNew } from 'comsMobx';
import { WeaLocaleProvider, WeaTools } from 'ecCom';
import { observable, action, runInAction } from 'mobx';
import { Base } from '../base';

const { getLabel } = WeaLocaleProvider;
const { TableStore } = WeaTableNew;

export default class UpdateOperatorStore extends Base {
	@observable signListStore = new TableStore();

	/**
	 * 需要提交的参数
	 * @type {{doneSk: Array, todoSk: Array, myTodo: string}}
	 */
	@observable submitparams = {};
	/**
	 * 核心参数从后台获取
	 * @type {{isManager: boolean, showSignInput: boolean, signInputRequired: boolean, showMyTodo: boolean, showDone: boolean, showTodo: boolean, title: string}}
	 */
	@observable coreparams = {
		datas: [],
		requestid: '', // 流程id
		workflowid: '',
		nodeid: '', // 当前节点
		nodeOptions: [],
		deleteids: [], // 删除的数据，进行撤回
	}

	@observable loading = false;
	// 数据调整次数
	changenumber = 0;

	/**
	 * 临时存储选择的节点以及人员
	 * @type {{}}
	 */
	@observable selected = {};

	allbuttons = [];
	@observable form = new WeaForm();
	initform = false;
	condition;

	workparams = {};
	@action
	init = (contentparams = {}) => {
		let params = { requestid: 10059057, workflowid: 55022, nodeid: 46527, dialogType: 1, ...contentparams };
		this.workparams = contentparams;
		this.changeCoreParams(params);
		this.loaddatas();
	}

	loaddatas = () => {
		this.loading = true;
		let params = { ...this.workparams, ...this.coreparams };

		// 不需要传递的参数
		delete params.datas;
		delete params.nodeOptions;
		params = { ...params, ...this.form.getFormParams() };
		WeaTools.callApi('/api/odoc/updateOperator/loadData', 'POST', params).then((res) => {
			const { api_status = false, status = false } = res;
			if (api_status || status) {
				const {
					nodeOptions = [], requestid, workflowid, nodeid,
					dialogType, sessionkey, conditions,
				} = res;
				runInAction(() => {
					this.coreparams = { nodeOptions, requestid, workflowid, nodeid, dialogType };
					// 如果没有选中数据默认选择一个节点
					const { nodeId } = this.selected;
					if (!nodeId && nodeOptions.length > 0) {
						const { key, showname } = nodeOptions[0];
						this.changeSelectNode(key, showname);
					}

					this.submitparams = { requestid, workflowid, nodeid, dialogType };
					this.signListStore = new TableStore();
					this.signListStore.getDatasCallback = () => {
						this.loading = false;
					};
					this.signListStore.getDatas(sessionkey, 1);

					if (!this.initform) { // 防止二次渲染form
						this.condition = conditions;
						this.form.initFormFields(conditions);
						this.initform = true;
					}
				});
			} else {
			}
		});
	}

	// 减签
	reducedSignature = (param = {}) => {
		let params = { ...this.workparams, ...this.submitparams, ...param };
		if (!(params && params.sltids)) {
			this.showErrorMsg('请选择减签数据！');
			return;
		}
		const { showcontent } = params;
		this.showConfirm(null, showcontent || getLabel(520037, '确认减签?'), () => {
			delete params.showcontent;
			this.loading = true;
			WeaTools.callApi('/api/odoc/updateOperator/reducedSignature', 'POST', params).then((res) => {
				const { api_status = false, status = false, api_message } = res;
				this.loading = false;
				if (api_status || status) {
					this.loaddatas();
					this.changenumber++;
					this.showSuccessMsg(api_message || getLabel(520035, '减签成功！'));
				} else {
					this.showErrorMsg(api_message);
				}
			});
		});
	}

	/**
	 * 选择下一个节点
	 * @param key
	 * @param value
	 */
	changeSelectNode = (key, value) => {
		// 第一步清空数据
		let slted = { nodeId: key, nodeName: value };
		// 第二步:为浏览按钮准备sqlwhere参数
		this.selected = { ...this.selected, ...slted };
	}
	changeSelectHrm = (ids, names, datas) => {
		let hrm = { userIds: ids, userNames: names, userList: datas };
		this.selected = { ...this.selected, ...hrm };
	}


	changeCoreParams = (params) => {
		this.coreparams = { ...this.coreparams, ...params };
	}

	/**
	 * 是否显示选中框
	 * @param rowSelection
	 * @returns {{}}
	 */
	getOpratorSetRowSelection = (rowSelection) => {
		let sel = { ...rowSelection };
		sel.getCheckboxProps = () => ({ disabled: false });
		return sel;
	}

	/**
	 * 流程流转
	 * @param param
	 * @param callback
	 */
	flowNext(callback) {
		const { nodeId = '', nodeName = '', userIds = '', userNames = '', userList = [] } = this.selected;
		let param = { nodeId, nodeName, userIds, userNames, userList };
		let datas = [];
		datas.push(param);

		let params = { ...this.workparams, ...this.submitparams, nextDirections: JSON.stringify(datas), sltids: this.coreparams.sltids };
		this.loading = true;
		// WeaTools.callApi('/api/odoc/odocrequest/flowNext', 'POST', params).then(res => {
		WeaTools.callApi('/api/odoc/updateOperator/plusSignature', 'POST', params).then((res) => {
			const { api_message, api_status = false, archived = false } = res;
			this.loading = false;
			if (api_status) { // 提交成功
				if (archived) { // 流程已归档需要强制刷新表单
					this.loading = true;
					this.showSuccessMsg(getLabel(522302, '加签成功，流程已归档！'), 3, () => {
						window.WfForm && window.WfForm.reloadPage();
					});

					return;
				}
					this.showSuccessMsg(getLabel(520004, '加签成功！'));

				this.loaddatas();
				this.changenumber++;
				typeof callback == 'function' && callback();
			} else {
				this.showErrorMsg(api_message || getLabel(520003, '加签失败！'));
			}
		});
	}

	/**
	 * 关闭销毁方法
	 */
	destory = () => {
		this.submitparams = [];
		this.selected = {};
		this.coreparams = {};
		this.form.reset();
	}
}
